package com.young.ums.web;

import com.young.common.client.ums.UmsClientHelper;
import com.young.common.core.excel.ExcelExport;
import com.young.common.core.excel.ExcelImport;
import com.young.common.core.excel.IExcelExport;
import com.young.common.core.excel.IExcelImport;
import com.young.common.util.CommonUtil;
import com.young.common.util.Result;
import com.young.common.util.StringUtils;
import com.young.common.util.Page;
import com.young.interfaces.log.annotation.Log;
import com.young.interfaces.ums.model.User;
import com.young.ums.model.Button;
import com.young.ums.service.IButtonService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
* 按钮信息服务
* Created by imrookie on 2018-10-28.
*/
@Controller
@RequestMapping(value = "/ums/button", method = {RequestMethod.GET, RequestMethod.POST})
public class ButtonController {

    @Resource(name="buttonService")
    IButtonService service;

    /**
     * 获取数据列表
     * @param obj
     * @param request
     * @return
     */
    @Log("查询按钮信息列表")
    @RequestMapping("/getList")
    @ResponseBody
    public Object getList(Button obj, HttpServletRequest request) {
        List<Button> list = service.getList(obj);
        return Result.buildSuccess(list);
    }

    /**
     * 获取分页数据
     * @param obj
     * @param request
     * @return
     */
    @Log("分页查询按钮信息列表")
    @RequestMapping("/getPage")
    @ResponseBody
    public Object getPage(Button obj, HttpServletRequest request) {
        Page page = CommonUtil.getPageFromRequest(request);
        List<Button> list = service.getPage(obj, page);
        page.setData(list);
        return page;
    }

    /**
     * 获取单条数据
     * @param id
     * @param request
     * @return
     */
    @Log("查询按钮信息")
    @RequestMapping("/get")
    @ResponseBody
    public Object get(String id, HttpServletRequest request) {
        Button obj = service.get(id);
        return Result.buildSuccess(obj);
    }

    /**
     * 统计数量
     * @param obj
     * @param request
     * @return
     */
    @Log("统计按钮信息数量")
    @RequestMapping("/count")
    @ResponseBody
    public Object count(Button obj, HttpServletRequest request) {
        return Result.buildSuccess(service.count(obj));
    }

    /**
     * 新增
     * @param obj
     * @param request
     * @return
     */
    @Log("新增按钮信息")
    @RequestMapping("/insert")
    @ResponseBody
    public Object insert(Button obj, HttpServletRequest request) {
        User user = UmsClientHelper.getLoginUser();
        if (user != null){
            obj.setCreateUserId(user.getId());
            obj.setUpdateUserId(user.getId());
        }
        service.insert(obj);
        return Result.buildSuccess();
    }

    /**
     * 修改
     * @param obj
     * @param request
     * @return
     */
    @Log("修改按钮信息")
    @RequestMapping("/update")
    @ResponseBody
    public Object update(Button obj, HttpServletRequest request) {
        User user = UmsClientHelper.getLoginUser();
        if (user != null){
            obj.setCreateUserId(null);//不修改创建人
            obj.setUpdateUserId(user.getId());
        }
        service.update(obj);
        return Result.buildSuccess();
    }

    /**
     * 删除
     * @param ids
     * @param id
     * @param request
     * @return
     */
    @Log("删除按钮信息")
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(String ids, String id, HttpServletRequest request) {
        if(StringUtils.isNotBlank(ids)) {
            String[] idArr = ids.split(",");
            service.delete(idArr);
        }else if(StringUtils.isNotBlank(id)){
            service.delete(id);
        }else{
            return Result.buildFail("删除失败:待删除编号无效!");
        }
        return Result.buildSuccess();
    }

    /**
     * 导入
     * @param file
     * @param request
     * @return
     */
    @Log("导入按钮信息")
    @RequestMapping("/import")
    @ResponseBody
    public Object importExcel(MultipartFile file, HttpServletRequest request) {
        try {
            if (file != null && !file.isEmpty()) {
                //excel导入处理,返回excel中的数据集合
                IExcelImport ei = new ExcelImport(file);//将文件转为ExcelImport对象
                //从excel读取数据
                List<Button> list = ei.getImportDataAsBean(Button.class);
                if (list != null && list.size() > 0){
                    int num = service.batchInsert(list);//批量插入
                    return Result.buildSuccessInfo("成功导入数据" + num + "条!");
                }else {
                    return Result.buildFail("导入失败:excel解析后结果为空!");
                }
            } else {
                return Result.buildFail("导入失败:文件为空!");
            }
        } catch (Exception e) {
            return Result.buildFail("导入失败!");
        }
    }

    /**
     * 导出
     * @param obj 查询参数
     * @param request
     * @param response
     * @throws Exception
     */
    @Log("导出按钮信息")
    @RequestMapping("/export")
    public void exportExcel(Button obj, HttpServletRequest request, HttpServletResponse response) throws Exception {
        List<Button> list = service.getList(obj);
        if (list != null && list.size() > 0){
            IExcelExport ee = new ExcelExport();
            ee.insertBeanList(list, Button.class);
            ee.write2Response(response, "excel_" + System.currentTimeMillis() + ".xls");
        }else{
            response.getWriter().write("数据为空!");
        }
    }

    /**
     * 查询按钮分组列表
     * @return
     */
    @Log("查询按钮分组列表")
    @RequestMapping("/getButtonCategoryList")
    @ResponseBody
    public Object getButtonCategoryList() {
        List<String> list = service.getButtonCategoryList();
        return Result.buildSuccess(list);
    }

    /**
     * 根据资源ID查询按钮列表
     * @param navigateId 资源导航编号
     * @param request
     * @return
     */
    @Log("根据资源ID查询按钮列表")
    @RequestMapping("/getButtonListByNavigateId")
    @ResponseBody
    public Object getButtonListByNavigateId(String navigateId, HttpServletRequest request) {
        List<Button> list = service.getButtonListByNavigateId(navigateId);
        return Result.buildSuccess(list);
    }

    /**
     * 资源详情快捷创建按钮
     * @param obj
     * @param navigateId
     * @param request
     * @return
     */
    @Log("资源详情快捷创建按钮")
    @RequestMapping("/insertAndRelevanceNavigate")
    @ResponseBody
    public Object insertAndRelevanceNavigate(Button obj, String navigateId, HttpServletRequest request) {
        User user = UmsClientHelper.getLoginUser();
        if (user != null){
            obj.setCreateUserId(user.getId());
            obj.setUpdateUserId(user.getId());
        }
        service.insertAndRelevanceNavigate(obj, navigateId);
        return Result.buildSuccess();
    }


    /**
     * 获取资源下权限范围内的按钮
     * @param navigateId 资源id
     * @param request
     * @return
     */
    @Log("获取资源下权限范围内的按钮")
    @RequestMapping("/getButtonListInNavigate")
    @ResponseBody
    public Object getButtonListInNavigate(String navigateId, HttpServletRequest request) {
        if (StringUtils.isBlank(navigateId)){
            return Result.buildFail("资源编号无效");
        }
        User user = UmsClientHelper.getLoginUser();
        if (user == null){//用户无效则返回回话超时
            return Result.build(Result.CodeEnum.SESSION_TIMEOUT);
        }

        return Result.buildSuccess(service.getButtonListInNavigate(navigateId, user.getId()));
    }

    /**
     * 批量克隆按钮并关联
     * @param buttonIds 待克隆的按钮编号,多个使用都好拼接
     * @param navigateId 资源编号,与克隆后的按钮关联
     * @param category 按钮分组
     * @param request
     * @return
     */
    @Log("批量克隆按钮并关联")
    @RequestMapping("/cloneButtonAndRelevanceNavigate")
    @ResponseBody
    public Object cloneButtonAndRelevanceNavigate(String buttonIds, String navigateId, String category, HttpServletRequest request) {
        if (StringUtils.isBlank(buttonIds) || StringUtils.isBlank(navigateId)){
            return Result.buildFail("按钮编号和资源编号不允许为空");
        }
        User user = UmsClientHelper.getLoginUser();
        if (user == null){
            return Result.build(Result.CodeEnum.SESSION_TIMEOUT);
        }
        service.cloneButtonAndRelevanceNavigate(buttonIds.split(","), navigateId, user.getId(), category);
        return Result.buildSuccess();
    }

}